package com.lanyam.travel.base.controller;


import com.lanyam.travel.base.utils.JSONResult;
import com.lanyam.travel.base.entity.DistrTramMember;
import com.lanyam.travel.base.entity.vo.DistrTramMemberVo;
import com.lanyam.travel.base.service.IDistrTramMemberService;
import com.lanyam.travel.base.utils.JsonMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.core.type.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;


@RestController
@RequestMapping("/distrTramMember")
public class DistrTramMemberController {

    @Autowired
    private IDistrTramMemberService distrTramMemberService;

    /**
     * 分页查询数据
     */
    @GetMapping("/getPage")
    public IPage<DistrTramMemberVo> getPage (String queryMap,
                                                @RequestParam(name="page", defaultValue = "1") Integer page,
                                                @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        IPage<DistrTramMemberVo> pageInfo = distrTramMemberService.getPageDistrTramMember(query, page, rows);
        return pageInfo;
    }

    /**
     * 按照团队id获取用户详情，如是分销用户也取得分销用户详情
     * 分页查询数据，多表联查
     *
     */
    @GetMapping("/getPageOfUserInfoByTeamId")
    public IPage<DistrTramMemberVo> getPageOfUserInfoByTeamId (String queryMap,
                                             @RequestParam(name="page", defaultValue = "1") Integer page,
                                             @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        IPage<DistrTramMemberVo> pageInfo = distrTramMemberService.getPageOfUserInfoByTeamId(query, page, rows);
        return pageInfo;
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public JSONResult add (@RequestBody DistrTramMember distrTramMember) {
        JSONResult result = null;
        try{
            distrTramMemberService.addDistrTramMember(distrTramMember);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 编辑数据
     */
    @PostMapping("/update")
    public JSONResult update (@RequestBody DistrTramMember distrTramMember) {
        JSONResult result = null;
        try{
            distrTramMemberService.updateDistrTramMember(distrTramMember);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 删除数据
     */
    @PostMapping("/delById")
    public JSONResult delById (String id) {
        JSONResult result = null;
        try {
            distrTramMemberService.delById(id);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }
}